﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ManyinEngine;

namespace WindowsFormsApplication1
{
    public partial class SemiCanvas : Panel
    {
        private List<Tile> _lstMap;

        public List<Tile> LstMapa
        {
            get { return _lstMap; }
            set { _lstMap = value; }
        }

        public SemiCanvas()
        {
            LstMapa = new List<Tile>();
            AutoScroll = true;
            DoubleBuffered = true;
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            //var myPen = new Pen(Color.Red);
            //e.Graphics.DrawLine(myPen, 0, 0, 200, 200);
            //myPen.Dispose();

            foreach (var node in _lstMap)
            {
                // drow hex from x;y
                var points = GetPointFsFromNode(node);
                var brush = new SolidBrush(Color.Blue);
                e.Graphics.FillPolygon(brush, points);
            }

            e.Graphics.TranslateTransform(AutoScrollPosition.X, AutoScrollPosition.Y);
            base.OnPaint(e);
        }

        private PointF[] GetPointFsFromNode(Tile tile)
        {
            var ret = new PointF[tile.Sides];
            var a = (Math.PI * 2) / tile.Sides;
            ret[0] = new PointF(tile.CenterX + tile.Radius - tile.BorderThickness, tile.CenterY + 0);
            for (int i = 1; i < tile.Sides; i++)
            {
                ret[i] = new PointF(tile.CenterX + Convert.ToSingle((tile.Radius - tile.BorderThickness) * Math.Cos(a * i)), tile.CenterY + Convert.ToSingle((tile.Radius - tile.BorderThickness) * Math.Sin(a * i)));
            }
            return ret;
        }
    }
}
